/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.LibrosForm;
import com.uca.sisbec.helpers.manto.LibroHelper;
import org.apache.struts.action.*;

/**
 *
 * @author Jorgore
 */
public class LibrosAction extends HibernateAction{
    
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception{
        request.setAttribute("save","new");
        return mapping.findForward("success");
    }
    
    public ActionForward initBusqueda(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception{
        return mapping.findForward("search");
    }
    
    public ActionForward agregarLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
       
        LibrosForm lf = (LibrosForm) form;
        LibroHelper helper = new LibroHelper(getSession(request));
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        
        if (!helper.validLibro(lf.getIsbn())){
            helper.InsertarLibroBD(lf);
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("libro.nuevo"));
            request.setAttribute("save", "update");
            request.setAttribute("msg", msg);
        }else{
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("libro.existe",lf.getIsbn()));
            request.setAttribute("save", "new");
            request.setAttribute("errors", errors);
        }
        return mapping.findForward("success");
    }
    
    public ActionForward verLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        try{
            LibroHelper hp = new LibroHelper(getSession(request));
            request.setAttribute("librosList",hp.getLibroList());
            return mapping.findForward("success");            
        }catch(Exception e){
            
            request.setAttribute("exception",e);
            return mapping.findForward("error");
        }
    }
    
    public ActionForward buscarLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        LibrosForm lf = (LibrosForm) form;
        LibroHelper hp = new LibroHelper(getSession(request));
        request.setAttribute("librosList", hp.getbusquedaLibroList(lf));
        return mapping.findForward("search");
    }
    
    public ActionForward modificarLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        LibrosForm lf = (LibrosForm) form;
        LibroHelper helper = new LibroHelper(getSession(request));
        helper.modificarLibroBD(lf);
        ActionMessages msg = new ActionMessages();
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("libro.modificado"));
        request.setAttribute("msg", msg);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward llenarForm(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        LibrosForm lf = (LibrosForm) form;
        LibroHelper lh= new LibroHelper(getSession(request));
        String isbn = (String)request.getParameter("isbn");
        lh.getLibroBD(lf, isbn);
        return mapping.findForward("success");
    }
}
